
package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.ProductBOInterface;
import org.cuatrovientos.erp.model.Product;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * ProductController
 * @author CaselHunter
 */
@Controller
public class ProductController {

	//Get log4j logger
	private static final Logger logger = 
			Logger.getLogger(ProductController.class);

	@Autowired
	private ProductBOInterface productBO;

	/**
	 * default constructor
	 */
	public ProductController () {
		logger.info("Product BEAN instantiated");
	}

	/**
	 * handles default /product or /product/list request
	 * @return
	 */
	@RequestMapping({"/product","/product/list"})
	public ModelAndView productDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Product> productList = productBO.selectAll();
		model.put("cusmtomers", productList );
		
		return new ModelAndView("ProductList", model);     
	}


	/**
	 * handles default /product/search request
	 * @param name
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/product/search"})
	public ModelAndView search(
			@RequestParam(value="name", defaultValue="", required=true)
			String name) {
		
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Product> productList = productBO.search(name);
		model.put("products", productList);
		
		return new ModelAndView("ProductList", model);     
	}

	/**
	 * handles default /product/delete request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/product/delete"})
	public String deleteProduct(
			@RequestParam(value="id", defaultValue="", required=true) int id) {
		
		logger.info("delete " + id);

		productBO.deleteById(id);
		
		return "redirect:/product";
	}

	/**
	 * handles default /product/new request
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/product/new"})
	public ModelAndView newProduct() {
		logger.info("new product");
		
		Product product = new Product();

		return new ModelAndView("NewProduct", "product", product);
	}

	/**
	 * handles default /product/save request
	 * @param product
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/product/save"})
	public String newProduct(@Valid Product product, BindingResult bindingResult) {
		logger.info("new product " + product.toString());

		//In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("product", product);
			
			return "NewProduct";
		}

		productBO.insert(product);
		
		return "redirect:/product";
	}

	/**
	 * handles default /product/detail request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/product/detail"})
	public ModelAndView productDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("Detail " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Product product = productBO.selectById(id);
		model.put("product", product);

		return new ModelAndView("ProductDetail", model);
	}


	/**
	 * handles default /product/update request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/product/update"})
	public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("update " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Product product = productBO.selectById(id);
		model.put("product", product);
		
		return new ModelAndView("UpdateProduct",model);
	}


	/**
	 * handles default /product/saveupdate request
	 * @param product
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/product/saveupdate"})
	public String saveUpdate(@Valid Product product, BindingResult bindingResult) {
		logger.info("update product " + product.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("product", product);
			
			return "UpdateProduct";
		}

		productBO.update(product);
		
		return "redirect:/product";
	}
	
}
